﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using GameLayer;
using GameInterface;
using Microsoft.Xna.Framework.Content;
using GameInstrument;
using GameBase;
using GameCharacter;
using Microsoft.Xna.Framework.Audio;
using MainGame;

namespace GameInstrument
{
    public abstract class Weapon : Button, ICollision<Sprite>, ICollision<Character>
    {
        private int _power;

        public virtual int Power
        {
            get { return _power; }
            set { _power = value; }
        }
        public Button Own { get; set; }

        public List<Fire> Fires { get; set; }
        public Vector2 Central { get; set; }
        public bool Exploding { get; set; }

        protected SoundEffectInstance SoundExplosize;
        public Weapon(ContentManager content)
        {
            this.Content = content;
            this.Fires = new List<Fire>();
            this.SoundExplosize = SoundManager.Explosive.CreateInstance();
        }

        public virtual void Explosive()
        {
            throw new NotImplementedException();
        }
        protected override void UpdateEntitys(GameTime gameTime, object extraData)
        {
            base.UpdateEntitys(gameTime, extraData);
            if (Exploding)
            {
                int temp = 0;
                this.Fires.ForEach(f =>
                {
                    if (!f.Visible)
                    {
                        temp++;
                        return;
                    }

                    f.Update(gameTime, extraData);
                });
                this.Visible = !(temp == this.Fires.Count);
            }
        }



        public virtual bool CollisionWith(Sprite other, bool pixelLevel)
        {
            throw new NotImplementedException();
        }


        public virtual bool CollisionWith(Character other, bool pixelLevel)
        {
            if (!this.Visible || !other.Visible)
                return false;
            if (Exploding)
            {
                foreach (var f in this.Fires)
                {
                    f.Position += this.Position;
                    bool ret = f.CollisionWith(other, pixelLevel);
                    f.Position -= this.Position;
                    if (ret == true)
                        break;
                }
            }
            else
            {
                return other.CollisionWith(this.Items[this.Index], pixelLevel);
            }
            return false;
        }

        public ContentManager Content { get; set; }
    }
}